package com.aaa.fees.service.ipml;

import cn.hutool.core.bean.BeanUtil;
import com.aaa.entity.BackfeeItem;
import com.aaa.entity.CareHistory;
import com.aaa.entity.CareOrder;
import com.aaa.entity.CareOrderItem;
import com.aaa.fees.dao.*;
import com.aaa.fees.service.BBackfeeService;
import com.aaa.fees.vo.PostObjVo;
import com.aaa.fees.vo.PostObjVo1;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class BBackfeeServiceImpl  implements BBackfeeService {
    @Autowired
    PayChargeDao payChargeDao;
    @Autowired
    BackfeesDao backfeesDao;
    @Autowired
    CareOrderDao careOrderDao;
    @Autowired
    BackfeesItemDao backfeesItemDao;
    @Autowired
    CareOederItemDao careOederItemDao;
    @Override
    public Result<Map<String, Object>> getBackRegId(String regId) {

        //根据前端穿过来的reg_id查询病历表信息
        QueryWrapper<CareHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("reg_id", regId);
        CareHistory careHistory = payChargeDao.selectOne(wrapper);
        if (careHistory == null) {
            return new Result<>(5000, "请输入已支付的挂号单", null);
        }
        //获取到ch_id查询药用处方表信息
        String chId = careHistory.getChId();
        QueryWrapper<CareOrder> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("ch_id", chId);
        List<CareOrder> careOrders = careOrderDao.selectList(wrapper1);
        List<Map<String, Object>> list = new ArrayList<>();
        if (careOrders.size() > 0) {
            for (CareOrder careOrder : careOrders) {
                Map<String, Object> map = BeanUtil.beanToMap(careOrder);
                QueryWrapper<CareOrderItem> wrapper2 = new QueryWrapper<>();
                wrapper2.eq("co_id", careOrder.getCoId());
                wrapper2.eq("status", 1);
                List<CareOrderItem> careOrderItems = careOederItemDao.selectList(wrapper2);
                //计算订单总金额
                if (careOrderItems.size() != 0) {
                    Double sum = careOrderItems.stream().map(item -> item.getAmount().doubleValue()).reduce((a, b) -> a + b).get();
                    map.put("careOrderItems", careOrderItems);
                    map.put("sum", sum);
                    list.add(map);
                }
            }

        }
        if (list.isEmpty()) {
            return new Result<>(5000, "请输入已支付的订单ID", null);
        } else {
            //把数据存放在map中返回
            Map<String, Object> maps = new HashMap<>();
            maps.put("careHistory", careHistory);
            maps.put("list", list);
            return new Result<>(200, "查询数据成功", maps);
        }
    }

    //现金退款
    @Override
    @Transactional
    public Result<CareOrderItem> createOrderChargeWithCash(PostObjVo postObjVo) {
        //获取挂号单id
        String regId=postObjVo.getOrderChargeDto1().getRegId();
        //获取患者姓名
        String patientName=postObjVo.getOrderChargeDto1().getPatientName();
        //获取总金额
        BigDecimal backAmount = postObjVo.getOrderChargeDto1().getBackAmount();
        //创建时间
        LocalDateTime createTime = LocalDateTime.now();
        //生成backId
        String backId="ODB";
        Random random = new Random();
        for (int i = 0; i <19 ; i++) {
            backId+=String.valueOf(random.nextInt(10));
        }
        //将前端传过来的数据存在退费表 his_order_backfee
        backfeesDao.insertAll(backId,regId,patientName,backAmount,createTime);

        //获取itemId  修改退费状态
        List<PostObjVo1> orderChargeItemDtoList = postObjVo.getOrderChargeItemDtoList();
        for (int i=0;i<orderChargeItemDtoList.size();i++){
            //退成功 修改开诊细表状态为已退费 his_care_order_item
            backfeesDao.updateByStatus1(orderChargeItemDtoList.get(i).getItemId());
        }
        for (int i=0;i<orderChargeItemDtoList.size();i++){
            BackfeeItem backfeeItem = new BackfeeItem();
            backfeeItem.setBackId(backId);
            backfeeItem.setItemId(orderChargeItemDtoList.get(i).getItemId());
            backfeeItem.setItemType(orderChargeItemDtoList.get(i).getItemType());
            backfeeItem.setItemName(orderChargeItemDtoList.get(i).getItemName());
            backfeeItem.setItemNum(orderChargeItemDtoList.get(i).getItemNum());
            backfeeItem.setItemPrice(orderChargeItemDtoList.get(i).getItemPrice());
            backfeeItem.setItemAmount(orderChargeItemDtoList.get(i).getItemAmount());
            backfeeItem.setCoId(orderChargeItemDtoList.get(i).getCoId());
            backfeeItem.setStatus("1");
            //退费成功将数据存放在退费表查看弹层的退付订单详情页 his_order_backfee_item
            backfeesItemDao.insert(backfeeItem);
        }
        return new Result<>(200,"退款成功");
    }

}

